import path from "node:path";
import { fileURLToPath } from "node:url";
import * as child_process from "node:child_process";
import fs from "node:fs";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const _7zBin = path.resolve(__dirname, `../thirdparty/7z/${process.platform}/${process.arch}/7za`);
console.log("7z bin =>", _7zBin);
console.log("exists =>", fs.existsSync(_7zBin));

const proc = child_process.spawn(
  _7zBin,
  [
    "x",
    "C:\\Dev\\Denv\\NodeJS\\node-v22.5.1-win-x64.7z",
    "-oC:\\Dev\\Denv\\NodeJS\\node-v22.5.1-win-x64",
    "-y",
    // ...
  ],
  { windowsHide: true /*, stdio: "inherit"*/ },
);

proc.stdout.on("data", data => {
  console.log(`stdout: ${data}`);
});

proc.stderr.on("data", data => {
  console.error(`stderr: ${data}`);
});

proc.on("exit", (code, signal) => {
  console.log(`child process terminated due to receipt of signal ${signal}, code: ${code}`);
});

setTimeout(() => {
  proc.kill();
}, 1000);
